<?php

/**
 * user actions.
 *
 * @package    PHOTO_GALLERY
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
////////// BeFORe POST //////////////////////////
        
        $limt = 3;
        $this->contactForm = new UsersLoginForm();
        $captcha_ed = Doctrine_Core::getTable('BlockedIps')->createQuery()->select()->where('ip = ?', $_SERVER['REMOTE_ADDR'])->fetchOne();
        $logincounter = intval($this->getUser()->getAttribute('logintrials', 1));
        $ip = $_SERVER['REMOTE_ADDR'];

     //   echo "COUNT" . $logincounter . " END<br/>";
     //   echo "captchaed===" . $captcha_ed . " END<br/>";

        if ($logincounter > $limt || $captcha_ed) {
            //add captcha  
            $this->contactForm->setWidget('_recaptcha_form', new sfWidgetFormReCaptcha(array(
                        'lang' => 'en',
                        'theme' => 'red',
                        'tabindex' => '0',
                        'custom_theme_widget' => null, // HTML ID of container for reCAPTCHA custom widget
                    )));
     //       echo "i am going to update wrongly now";
            
            BlockedIps::updateByIP($ip);
            $this->recap = new reCaptcha();
        }
        
//////////// ON POST /////////////////////
        if ($request->isMethod('post')) {

            if (isset($this->recap)) {

                $answer = $this->recap->checkAnswer($request)->is_valid;
                echo "ANSWER:$answer END<br/> ";
//
//                if ($answer) {
//                    $cap = TRUE;
//                    echo $answer;
//                } else {
//                    $cap = FALSE;
//                    echo $answer;
//                }
            }


            $formInput = $request->getParameter('users_login');
            $this->contactForm->bind($formInput);
            if ($this->contactForm->isValid()) {

                if (!isset($answer) || $answer) {
                    $user = UsersLogin::login($formInput['username'], $formInput['password']);
                    if ($user != FALSE) {
                        $user = $user[0];
                        $status = intval($user['status']);
                        if ($status == 0) {
                            $this->getUser()->setAttribute('userId', $user['user_id']); //user id in session
                            if ($request->getParameter('remember', false)) {
                                $this->getResponse()->setCookie('userId', $user['user_id'], time() + 60 * 60 * 24 * 15);
                            }
                            echo 'success';

                            //redirect to profile
                        } else if ($status == 1) {
                            //add msg u are suspended contact admin
                        } else {
                            //add u are banned from this site
                        }


                        if (isset($captcha_ed)) {
                            BlockedIps::deleteByIP($_SERVER['REMOTE_ADDR']);
                        }
                    } else {
                        $logincounter++;
                        $this->getUser()->setAttribute('logintrials', $logincounter);

                        if ($logincounter > $limt) {
                            //insert in database
                            $ip = $_SERVER['REMOTE_ADDR'];
                            BlockedIps::updateByIP($ip);
//                        
                        }
                    }
                } else {

                    $ip = $_SERVER['REMOTE_ADDR']; ///missing condition
                    BlockedIps::updateByIP($ip);
                }
            }
        }
    }

    public function executeReg($request) {
        /*
          $u = new UsersLogin();
          $u->setUsername('ziad2');
          $u->setPassword('12345678');
          $u->setStatus(0);
          $u->save();
         */
//       var_dump(sha1('12345678'));
//       echo '<br/>';
//       var_dump(md5('12345678'));
//       
//        
//    echo BlockedIps::findByIP($_SERVER['REMOTE_ADDR']);
//    $this->setTemplate('index');
//        
    }

}
